public class Main {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        Student stu1 = new Student(1,"zhangsan",23);
        Student stu2 = new Student(2,"lisi",24);
        Student stu3 = new Student(3,"wangwu",25);

        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;

        //先找到id为2的索引
        int index = getIndex(arr,2);
        if(index>=0){
            //年龄加1
            Student stu = arr[index];
            int newAge = stu.getAge()+1;
            stu.setAge(newAge);
            printArr(arr);
        }else{
            //不存在
            System.out.println("当前id不存在,请修改");
        }

        int index1=  getIndex(arr,10);
        if(index1>=0){
            //存在,删除
            arr[index1] = null;
        }
        else{
            System.out.println("当前id不存在,删除失败");
        }
        Student stu4 = new Student(4,"zhaosi",26);
        //创建一个新数组
        boolean flag = contain(arr,stu4.getId());
        if(flag){
            System.out.println("当前id重复,请修改");
        }else{
            int count = getCount(arr);
            if(count==arr.length){
                Student[] newArr =  creatNewArr(arr);
                newArr[count] = stu4;
                printArr(newArr);
            }else{
                arr[count] = stu4;
                printArr(arr);

            }
        }

    }
    public static Student[] creatNewArr(Student[] arr){
        Student[] newArr = new Student[arr.length+1];
        for(int i = 0;i<arr.length;i++){
            newArr[i] = arr[i];
        }
        return newArr;
    }
    public static int getCount(Student[] arr){
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                count++;
            }
        }
        return count;
    }
    public static boolean contain(Student[] arr,int id){
        //依次获取学生对象id
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu!=null) {
                int sid = stu.getId();
                if (sid == id) {
                    return true;
                }
            }
            }
        return false;
    }


    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu!=null){
                System.out.println(stu.getAge()+", "+ stu.getId()+", "+stu.getName());
            }
        }
    }
    //找到id数组中的索引

    public static int getIndex(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu!=null){
                int sid =  stu.getId();
                if(sid==id){
                    return i;
                }
            }
        }
        return -1;
    }
}
